package org.testmyapp.tasks;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.testmyapp.engine.IEngineServices;
import org.testmyapp.entities.Bill;
import org.testmyapp.entities.BillPosition;
import org.testmyapp.entities.RefundPart;
import org.testmyapp.global.Variable;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;

public class CheckRefunds implements TaskWorker {

	public String validate(IEngineServices aes,
			Map<String, String[]> parameterMap, String processInstanceId,
			BusinessObjectPersistenceProvider bopp) {
		String actionCheckManual = parameterMap.get("actionCheckManual")[0];
		
		aes.setVariable(processInstanceId, "actionCheckManual",
				actionCheckManual);
		
		if(actionCheckManual.equals("belegeKorrigieren")) {
			aes.setVariable(processInstanceId, Variable.REFUNDPARTLIST.getName(), new ArrayList<RefundPart>());
			return "";
		}

		String billPositionNrString = parameterMap.get("billPositionNr")[0];
		Integer billPositionNr = null;
		if (!(billPositionNrString == null || billPositionNrString.equals(""))) {
			billPositionNr = Integer.parseInt(billPositionNrString);
		}

		String billNrString = parameterMap.get("billNr")[0];
		Integer billNr = null;
		if (!(billNrString == null || billNrString.equals(""))) {
			billNr = Integer.parseInt(billNrString);
		}

		String refundPartNrString = parameterMap.get("refundPartNr")[0];
		Integer refundPartNr = null;
		if (!(refundPartNrString == null || refundPartNrString.equals(""))) {
			refundPartNr = Integer.parseInt(refundPartNrString);
		}


		@SuppressWarnings("unchecked")
		List<RefundPart> refundPartList = (List<RefundPart>) aes.getVariable(
				Variable.REFUNDPARTLIST.getName(), processInstanceId);
		Integer sum = 0;
		for (RefundPart rp : refundPartList) {
			try {
				sum += rp.getRefundSum();
			} catch (Exception e) {
				sum += 0;
			}
		}
		aes.setVariable(processInstanceId, "refundSum", sum);


		if (actionCheckManual.equals("endAbrechnung")) {
			return "";
		}

		if (billPositionNr == null || refundPartNr == null || billNr == null) {
			throw new RuntimeException(
					"Bitte rechnungsposition und erstattungspart angeben!");
		}

		@SuppressWarnings("unchecked")
		List<BillPosition> billPositions = (List<BillPosition>) aes
				.getVariable(Variable.POSITIONLIST.getName(), processInstanceId);
		@SuppressWarnings("unchecked")
		List<RefundPart> bills = (List<RefundPart>) aes.getVariable(
				Variable.BILLLIST.getName(), processInstanceId);

		if (billPositions == null || refundPartList == null || bills == null
				|| billPositions.isEmpty() || refundPartList.isEmpty()
				|| bills.isEmpty()) {
			throw new RuntimeException(
					"Technischer Fehler: Konnte keine variablen finden (rechnungsposten, rechnungen oder erstattungsparts!");
		}

		RefundPart refundPart = null;
		try {
			for (RefundPart rp : refundPartList) {
				if (rp.getRefundPartNr() == refundPartNr) {
					System.out.println("rp is correct;; now in:"
							+ billPositionNr + " from list: "
							+ rp.getBillPosition().getBillPositionNr());
					if (rp.getBillPosition().getBillPositionNr()
							.equals(billPositionNr)) {
						System.out.println("bp is correct");
						if (rp.getBillPosition().getBill().getBillNr()
								.equals(billNr)) {
							System.out.println("bill is correct");
							refundPart = rp;
						}
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("Bitte nummern korrekt angeben");
		}

		if (refundPart == null) {
			System.err.println("bill -> " + billNr + ", bp -> "
					+ billPositionNr + ", rp -> " + refundPartNr);
			System.err.println("rps: " + refundPartList);
			throw new RuntimeException("Bitte nummern korrekt angeben");
		}

		aes.setVariable(processInstanceId, "refundPartToCorrect", refundPart);

		return "";
	}

}
